<?php

/**
*
* @package ppkBB3cker
* @version $Id: cron.php 1.000 2010-06-14 17:52:00 PPK $
* @copyright (c) 2010 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/
define('IN_PHPBB', true);
define('IN_CRON', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Do not update users last page entry
$user->session_begin(false);
$auth->acl($user->data);

@set_time_limit(0);
ob_start();

// Output transparent gif
header('Cache-Control: no-cache');
header('Content-type: image/gif');
header('Content-length: 43');

echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');

header('Connection: close');

ob_end_flush();
@ob_flush();
flush();

$dt = time();
$id=request_var('id', array(0 => ''));
$ids=sizeof($id);

if($ids)
{
	//$db->sql_query("SET sql_mode='NO_UNSIGNED_SUBTRACTION'");

	$ppkbb_cron_options=my_split_config($config['ppkbb_cron_options'], 5, 'my_int_val');

	$ppkbb_cron_options[2] && $ids > 1 ? $id=array_slice($id, 0, 1) : '';

	$sql="SELECT * FROM ".TRACKER_CRON_TABLE." WHERE id IN('".implode("', '", array_map('my_int_val', $id))."') AND status='0'";
	$result=$db->sql_query($sql);

	while($row=$db->sql_fetchrow($result))
	{
		if($ppkbb_cron_options[3] && $dt-$row['added'] > $ppkbb_cron_options[3])
		{
			continue;
		}
		if($row['type']=='t_clean')
		{
			$db->sql_query("UPDATE ".TRACKER_CRON_TABLE." SET status='1' WHERE id='{$row['id']}'");
			$torrents_cleanup=unserialize(stripslashes($row['data']));
			if(is_array($torrents_cleanup) && sizeof($torrents_cleanup))
			{
				$sql="SELECT torrent torrent_id, SUM(IF(seeder='1', 1, 0)) seeder, SUM(IF(seeder='0', 1, 0)) leecher FROM ". TRACKER_PEERS_TABLE ." WHERE torrent IN('".(implode("', '", $torrents_cleanup))."') GROUP BY torrent";
				$result2=$db->sql_query($sql);
				$r_seeders_leechers=array();
				while($row_cleanup=$db->sql_fetchrow($result2))
				{
					$r_seeders_leechers[$row_cleanup['torrent_id']][]="seeders='".intval($row_cleanup['seeder'])."'";
					$r_seeders_leechers[$row_cleanup['torrent_id']][]="leechers='".intval($row_cleanup['leecher'])."'";
					$r_seeders_leechers[$row_cleanup['torrent_id']][]="lastcleanup='{$dt}'";
				}
				$db->sql_freeresult($result2);

				foreach($torrents_cleanup as $k => $v)
				{
					if(!isset($r_seeders_leechers[$k]))
					{
						$r_seeders_leechers[$k][]="seeders='0'";
						$r_seeders_leechers[$k][]="leechers='0'";
						$r_seeders_leechers[$k][]="lastcleanup='{$dt}'";
					}
				}

				foreach($r_seeders_leechers as $k => $t)
				{
					$sql="UPDATE ".TRACKER_TORRENTS_TABLE." SET ".implode(', ', $t)." WHERE id='{$k}'";
					$db->sql_query($sql);
				}

			}
			$db->sql_query('DELETE FROM '. TRACKER_PEERS_TABLE ." WHERE {$dt} - last_action > '{$config['ppkbb_tcdead_time']}'");
			set_config('ppkbb_tracker_last_cleanup', $dt, 1);
			//purge_tracker_config(true);
			$db->sql_query("DELETE FROM ".TRACKER_CRON_TABLE." WHERE id='{$row['id']}'");
		}
		else if($row['type']=='t_announce')
		{
			$db->sql_query("UPDATE ".TRACKER_CRON_TABLE." SET status='1' WHERE id='{$row['id']}'");
			$torrents_hashes=unserialize(stripslashes($row['data']));

			if(is_array($torrents_hashes) && sizeof($torrents_hashes))
			{
				$ppkbb_tcrannounces_options=my_split_config($config['ppkbb_tcrannounces_options'], 9, 'my_int_val');
				include($phpbb_root_path.'tracker/include/rannfunc.'.$phpEx);
				$torrents_id=array_keys($torrents_hashes['torrents_id']);
				$torrents_hashes=$torrents_hashes['torrents_id'];
				$rem_announces=0;
				$r_torr=$r_exs=$r_ann=$torrents_remote=$rem_announced=array();
				$sql='SELECT rt.id, rt.rtrack_url, rt.torrent torrent2, ra.torrent, ra.next_announce, ra.a_message, ra.s_message, ra.a_interval, ra.err_count, ra.seeders, ra.leechers, ra.times_completed FROM '.TRACKER_RTRACK_TABLE.' rt LEFT JOIN '.TRACKER_RANNOUNCES_TABLE." ra ON (rt.id=ra.tracker AND ra.torrent IN('".(implode("', '", $torrents_id))."')) WHERE (rt.zone_id='0' AND rt.rtrack_remote='1' AND rt.torrent='0') OR rt.torrent IN('".(implode("', '", $torrents_id))."')";
				$result2=$db->sql_query($sql);

				$r_lock=$ppkbb_tcrannounces_options[4] * ($ppkbb_tcrannounces_options[5] ? $ppkbb_tcrannounces_options[5] * sizeof($torrents_id) : 5 * sizeof($torrents_id));
				$result3=$db->sql_query("UPDATE ".TRACKER_RANNOUNCES_TABLE." SET next_announce='".($dt+$r_lock)."' WHERE torrent IN('".(implode("', '", $torrents_id))."') AND next_announce < {$dt}");
				while($row_remote=$db->sql_fetchrow($result2))
				{
					if(!$row_remote['torrent2'])
					{
						$r_torr['all'][$row_remote['id']]=$torrents_hashes;
					}
					else
					{
						isset($torrents_hashes[$row_remote['torrent2']]) ? $r_torr['torr'][$row_remote['id']][$row_remote['torrent2']]=$torrents_hashes[$row_remote['torrent2']] : '';
					}
					if(!isset($r_ann[$row_remote['id']]))
					{
						$rtrack_url=$row_remote['rtrack_url'];
						$r_ann[$row_remote['id']]['rtrack_url']=$rtrack_url;
					}
					if($row_remote['torrent'])
					{
						$r_exs[$row_remote['torrent2'].'_'.$row_remote['torrent']][$row_remote['id']]=$row_remote;
					}
				}
				$db->sql_freeresult($result2);

				if(isset($r_torr['all']))
				{
					foreach($r_torr['all'] as $tr_id => $a_data)
					{
						foreach($a_data as $t_id => $t_hash)
						{
							if(($ppkbb_tcrannounces_options[5] && @$rem_announced[$t_id] >= $ppkbb_tcrannounces_options[5]) || ($ppkbb_tcrannounces_options[8] && $rem_announces >= $ppkbb_tcrannounces_options[8]))
							{
								break;
							}
							$t_hash=hex2bin($t_hash);
							if(isset($r_exs['0_'.$t_id][$tr_id]))
							{
								if($dt > $r_exs['0_'.$t_id][$tr_id]['next_announce'])
								{
									$torrents_remote[$tr_id][$t_id]=array_merge($r_exs['0_'.$t_id][$tr_id], ($r_exs['0_'.$t_id][$tr_id]['s_message'] ? remote_announce($r_ann[$tr_id]['rtrack_url'], $t_hash) : remote_scrape($r_ann[$tr_id]['rtrack_url'], $t_hash)));
									@$rem_announced[$t_id]+=1;
									$rem_announces+=1;
								}
							}
							else
							{
								$torrents_remote[$tr_id][$t_id]=remote_scrape($r_ann[$tr_id]['rtrack_url'], $t_hash);
								@$rem_announced[$t_id]+=1;
								$rem_announces+=1;
							}
						}
					}
				}
				if(isset($r_torr['torr']))
				{
					foreach($r_torr['torr'] as $tr_id => $a_data)
					{
						foreach($a_data as $t_id => $t_hash)
						{
							if(($ppkbb_tcrannounces_options[5] && @$rem_announced[$t_id] >= $ppkbb_tcrannounces_options[5]) || ($ppkbb_tcrannounces_options[8] && $rem_announces >= $ppkbb_tcrannounces_options[8]))
							{
								break;
							}
							$t_hash=hex2bin($t_hash);
							if(isset($r_exs[$t_id.'_'.$t_id][$tr_id]))
							{
								if($dt > $r_exs[$t_id.'_'.$t_id][$tr_id]['next_announce'])
								{
									$torrents_remote[$tr_id][$t_id]=array_merge($r_exs[$t_id.'_'.$t_id][$tr_id], ($r_exs[$t_id.'_'.$t_id][$tr_id]['s_message'] ? remote_announce($r_ann[$tr_id]['rtrack_url'], $t_hash) : remote_scrape($r_ann[$tr_id]['rtrack_url'], $t_hash)));
									@$rem_announced[$t_id]+=1;
									$rem_announces+=1;
								}
							}
							else
							{
								$torrents_remote[$tr_id][$t_id]=remote_scrape($r_ann[$tr_id]['rtrack_url'], $t_hash);
								@$rem_announced[$t_id]+=1;
								$rem_announces+=1;
							}
						}
					}
				}

				if(is_array($torrents_remote) && sizeof($torrents_remote))
				{
					$r_check=array();
					foreach($torrents_remote as $tr_id => $k)
					{
						foreach($k as $t_id => $v)
						{
							$a_time=@!$v['interval'] || $v['interval'] < $ppkbb_tcrannounces_options[1]*60 ? $ppkbb_tcrannounces_options[0]*60 : $v['interval'];
							$a_time ? '' : $a_time=$config['ppkbb_tcannounce_interval'];
							if((isset($v['s_message']) && !$v['s_message']) || (isset($v['s_message']) && isset($v['a_message']) && !$v['a_message']) || (!isset($v['s_message']) && isset($v['a_message']) && !$v['a_message']))
							{
								if(@$v['next_announce'])
								{
									$result3=$db->sql_query("UPDATE ".TRACKER_RANNOUNCES_TABLE." SET seeders='".my_int_val($v['seeders'])."', leechers='".my_int_val($v['leechers'])."', times_completed='".my_int_val($v['times_completed'])."', next_announce='".($dt+$a_time)."', a_message='', s_message='', err_count='0', a_interval='{$a_time}' WHERE  tracker='{$tr_id}' and torrent='{$t_id}'");
								}
								else
								{
									if(!isset($r_check[$tr_id.'_'.$t_id]))
									{
										/*$result3=$db->sql_query("UPDATE ".TRACKER_RANNOUNCES_TABLE." SET seeders='".my_int_val($v['seeders'])."', leechers='".my_int_val($v['leechers'])."', peers='".my_int_val(@$v['peers'])."', times_completed='".my_int_val($v['times_completed'])."', next_announce='".($dt+$a_time)."', a_message='', s_message='', err_count='0', a_interval='{$a_time}' WHERE  tracker='{$tr_id}' and torrent='{$t_id}'");
										if(!$db->sql_affectedrows())
										{*/
											$result3=$db->sql_query("INSERT INTO ".TRACKER_RANNOUNCES_TABLE." (torrent, tracker, seeders, leechers, times_completed, next_announce, a_interval) VALUES('{$t_id}', '{$tr_id}', '".my_int_val(@$v['seeders'])."', '".my_int_val(@$v['leechers'])."', '".my_int_val(@$v['times_completed'])."', '".($dt+$a_time)."', '{$a_time}') ON DUPLICATE KEY UPDATE seeders='".my_int_val($v['seeders'])."', leechers='".my_int_val($v['leechers'])."', times_completed='".my_int_val($v['times_completed'])."', next_announce='".($dt+$a_time)."', a_message='', s_message='', err_count='0', a_interval='{$a_time}'");
										//}
										$r_check[$tr_id.'_'.$t_id]=1;
									}
								}
							}
							else
							{
								$mpl_a_time=$ppkbb_tcrannounces_options[6] ? $a_time * $ppkbb_tcrannounces_options[6] : $a_time;
								if(@$v['next_announce'])
								{
									$result3=$db->sql_query("UPDATE ".TRACKER_RANNOUNCES_TABLE." SET a_message='".$db->sql_escape(@$v['a_message'])."', s_message='".$db->sql_escape(@$v['s_message'])."', err_count=err_count+1, next_announce=(err_count*{$mpl_a_time})+".($a_time+$dt).", seeders='0', leechers='0', times_completed='0', a_interval='{$a_time}' WHERE tracker='{$tr_id}' AND torrent='{$t_id}'");
								}
								else
								{
									if(!isset($r_check[$tr_id.'_'.$t_id]))
									{
										/*$result3=$db->sql_query("UPDATE ".TRACKER_RANNOUNCES_TABLE." SET a_message='".$db->sql_escape(@$v['a_message'])."', s_message='".$db->sql_escape(@$v['s_message'])."', err_count=1, next_announce='".intval($dt+$mpl_a_time+$a_time)."', seeders='0', leechers='0', peers='0', times_completed='0', a_interval='{$a_time}' WHERE tracker='{$tr_id}' AND torrent='{$t_id}'");
										if(!$db->sql_affectedrows())
										{*/
											$result3=$db->sql_query("INSERT INTO ".TRACKER_RANNOUNCES_TABLE." (torrent, tracker, next_announce, a_message, a_interval, s_message, err_count) VALUES('{$t_id}', '{$tr_id}', '".intval($dt+$mpl_a_time+$a_time)."', '".$db->sql_escape(@$v['a_message'])."', '{$a_time}', '".$db->sql_escape(@$v['s_message'])."', '1') ON DUPLICATE KEY UPDATE a_message='".$db->sql_escape(@$v['a_message'])."', s_message='".$db->sql_escape(@$v['s_message'])."', err_count=1, next_announce='".intval($dt+$mpl_a_time+$a_time)."', seeders='0', leechers='0', times_completed='0', a_interval='{$a_time}'");
										//}
										$r_check[$tr_id.'_'.$t_id]=1;
									}
								}
							}
						}
					}
				}
				$sql="UPDATE ".TRACKER_TORRENTS_TABLE." SET
					".TRACKER_TORRENTS_TABLE.".rem_seeders=(SELECT SUM(".TRACKER_RANNOUNCES_TABLE.".seeders) FROM ".TRACKER_RANNOUNCES_TABLE." WHERE ".TRACKER_TORRENTS_TABLE.".id=".TRACKER_RANNOUNCES_TABLE.".torrent) ,
					".TRACKER_TORRENTS_TABLE.".rem_leechers=(SELECT SUM(".TRACKER_RANNOUNCES_TABLE.".leechers) FROM ".TRACKER_RANNOUNCES_TABLE." WHERE ".TRACKER_TORRENTS_TABLE.".id=".TRACKER_RANNOUNCES_TABLE.".torrent) ,
					".TRACKER_TORRENTS_TABLE.".rem_times_completed=(SELECT SUM(".TRACKER_RANNOUNCES_TABLE.".times_completed) FROM ".TRACKER_RANNOUNCES_TABLE." WHERE ".TRACKER_TORRENTS_TABLE.".id=".TRACKER_RANNOUNCES_TABLE.".torrent),
					".TRACKER_TORRENTS_TABLE.".lastremote='{$dt}'
					 WHERE ".TRACKER_TORRENTS_TABLE.".id IN('".implode("', '", $torrents_id)."');";
				$db->sql_query($sql);
			}

			$db->sql_query("DELETE FROM ".TRACKER_CRON_TABLE." WHERE id='{$row['id']}'");
		}
		else if($row['type']=='u_status')
		{
			$db->sql_query("UPDATE ".TRACKER_CRON_TABLE." SET status='1' WHERE id='{$row['id']}'");
			$u_status=unserialize(stripslashes($row['data']));
			if(is_array($u_status) && sizeof($u_status))
			{
				$db->sql_query("UPDATE ".TRACKER_TORRENTS_TABLE." SET forb='0' WHERE forb IN('".implode("', '", $u_status)."')");

				$db->sql_query("DELETE FROM ".TRACKER_CRON_TABLE." WHERE id='{$row['id']}'");
			}
		}
		else if($row['type']=='t_stat')
		{
			$ppkbb_tstat_ctime=$config['ppkbb_tstat_ctime'];
			$ppkbb_tcenable_rannounces=my_split_config($config['ppkbb_tcenable_rannounces'], 9, 'my_int_val');
			$total_rem_peers=$total_rem_leech=$total_rem_seed=0;
			if($ppkbb_tcenable_rannounces[0])
			{
				$sql="SELECT SUM(rem_seeders) seeder, SUM(rem_leechers) leecher FROM ".TRACKER_TORRENTS_TABLE."";
				$result3=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
				$total_rem_peer=$db->sql_fetchrow($result3);
				$total_rem_peers=my_int_val($total_rem_peer['seeder']+$total_rem_peer['leecher']);
				$total_rem_seed=my_int_val($total_rem_peer['seeder']);
				$total_rem_leech=my_int_val($total_rem_peer['leecher']);
			}

			$sql="SELECT SUM(IF(seeder='1',1,0)) seeder, SUM(IF(seeder='0',1,0)) leecher FROM ".TRACKER_PEERS_TABLE."";
			$result3=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
			$total_peer=$db->sql_fetchrow($result3);
			$total_peers=my_int_val($total_peer['seeder']+$total_peer['leecher']+$total_rem_peers);
			$total_seed=my_int_val($total_peer['seeder']+$total_rem_seed);
			$total_leech=my_int_val($total_peer['leecher']+$total_rem_leech);
			set_config('ppkbb_total_seed_leech', "{$total_seed} {$total_leech}");

			$sql="SELECT SUM(uploaded) upload, SUM(downloaded) download FROM ".TRACKER_SNATCHED_TABLE."";
			$result3=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
			$total_updown=$db->sql_fetchrow($result3);
			$total_up=my_int_val($total_updown['upload']);
			$total_down=my_int_val($total_updown['download']);
			set_config('ppkbb_total_up_down', "{$total_up} {$total_down}");

			$sql="SELECT SUM(uploaded) upload, SUM(downloaded) download FROM ".TRACKER_PEERS_TABLE."";
			$result3=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
			$total_supdown=$db->sql_fetchrow($result3);
			$total_sup=my_int_val($total_supdown['upload']);
			$total_sdown=my_int_val($total_supdown['download']);
			set_config('ppkbb_total_sup_sdown', "{$total_sup} {$total_sdown}");

			$sql="SELECT SUM(size) size FROM ".TRACKER_FILES_TABLE."";
			$result3=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
			$total_size=$db->sql_fetchrow($result3);
			$total_sizes=my_int_val($total_size['size']);
			set_config('ppkbb_total_peers_size', "{$total_peers} {$total_sizes}");

			$sql="SELECT COUNT(DISTINCT(torrent)) seeders, COUNT(DISTINCT(userid)) seeders2 FROM ".TRACKER_PEERS_TABLE." where seeder='1'";
			$result3=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
			$total_t_seeds=$db->sql_fetchrow($result3);
			$t_seed=my_int_val($total_t_seeds['seeders']);
			$sql="SELECT COUNT(DISTINCT(torrent)) leechers, COUNT(DISTINCT(userid)) leechers2 FROM ".TRACKER_PEERS_TABLE." where seeder='0'";
			$result3=$db->sql_query($sql, $ppkbb_tstat_ctime * 60);
			$total_t_leechs=$db->sql_fetchrow($result3);
			$t_leech=my_int_val($total_t_leechs['leechers']);
			set_config('ppkbb_total_tdown_tup', "{$t_seed} {$t_leech}");

			$u_seed=my_int_val($total_t_seeds['seeders2']);
			$u_leech=my_int_val($total_t_leechs['leechers2']);
			set_config('ppkbb_total_udown_uup', "{$u_seed} {$u_leech}");

			//if($dt - $config['ppkbb_trlast_stattime'] > $ppkbb_tstat_ctime * 60)
			//{
				$sql="SELECT SUM(s.uploadoffset/(s.last_action-s.prev_action)) up_speed, SUM(s.downloadoffset/(s.last_action-s.prev_action)) down_speed FROM ".TRACKER_SNATCHED_TABLE." s WHERE {$dt}-s.last_action < {$config['ppkbb_tcdead_time']}";
				$result3=$db->sql_query($sql, $ppkbb_tstat_ctime * 60, 'trstat#up_speed-down_speed');
				$total_updown_speed=$db->sql_fetchrow($result3);
				$up_speed=my_int_val($total_updown_speed['up_speed']);
				$down_speed=my_int_val($total_updown_speed['down_speed']);
				/*set_tracker_config('ppkbb_trlast_stattime', $dt);
				set_tracker_config('ppkbb_trstat_vals', "{$up_speed} {$down_speed}");
			}
			else
			{
				list($up_speed, $down_speed, )=@explode(' ', $config['ppkbb_trstat_vals']);
			}*/
			set_config('ppkbb_total_speedup_speeddown', "{$up_speed} {$down_speed}");

			set_config('ppkbb_last_stattime', $dt, 1);
			$db->sql_query("DELETE FROM ".TRACKER_CRON_TABLE." WHERE id='{$row['id']}'");
		}
		else if($row['type']=='u_update')
		{
			$db->sql_query("UPDATE ".TRACKER_CRON_TABLE." SET status='1' WHERE id='{$row['id']}'");
			$u_update=unserialize(stripslashes($row['data']));
			$user_tracker_data=$u_cron=array();
			$ppkbb_tcguests_enabled=my_split_config($config['ppkbb_tcguests_enabled'], 2, 'my_int_val');
			$user_tracker_data=my_split_config($u_update[4], 3, 'my_int_val');
			$u_update[3]=my_int_val($u_update[3]);
			if($u_update[0])
			{
				$sql='SELECT COUNT(t.topic_id) AS torr
					FROM ' . TOPICS_TABLE . ' t, '.TRACKER_TORRENTS_TABLE.' tt, '.FORUMS_TABLE." f
					WHERE tt.poster_id='{$u_update[3]}' AND t.topic_id=tt.topic_id AND f.forum_id=t.forum_id AND f.forumas=1";
				$result3=$db->sql_query($sql/*, $config['ppkbb_tstat_ctime']*60*/);
				$user_torr_data=$db->sql_fetchrow($result3);
				$user_torr_data['torr']=intval(@$user_torr_data['torr']);
				$db->sql_freeresult($result3);
				$u_cron[]="user_torrents='{$user_torr_data['torr']}'";
				$user_tracker_data[0]=$dt;

				$sql='SELECT SUM(tt.size) AS size
					FROM ' . TOPICS_TABLE . ' t, '.TRACKER_TORRENTS_TABLE.' tt, '.FORUMS_TABLE." f
					WHERE tt.poster_id='{$u_update[3]}' AND t.topic_id=tt.topic_id AND f.forum_id=t.forum_id AND f.forumas=1";
				$result3=$db->sql_query($sql/*, $config['ppkbb_tstat_ctime']*60*/);
				$user_torrsize_data=$db->sql_fetchrow($result3);
				$user_torrsize_data['size']=my_int_val(@$user_torrsize_data['size']);
				$db->sql_freeresult($result3);
				$u_cron[]="user_torrents_size='{$user_torrsize_data['size']}'";
			}
			if($u_update[1])
			{
				$sql = 'SELECT COUNT(p.post_id) AS comm
					FROM ' . TOPICS_TABLE . ' t, '.POSTS_TABLE.' p, '.FORUMS_TABLE." f
					WHERE p.poster_id='{$u_update[3]}' AND t.topic_id=p.topic_id AND t.topic_first_post_id!=p.post_id AND f.forum_id=t.forum_id AND f.forumas=1";
				$result3=$db->sql_query($sql/*, $config['ppkbb_tstat_ctime']*60*/);
				$user_comm_data=$db->sql_fetchrow($result3);
				$user_comm_data['comm']=intval(@$user_comm_data['comm']);
				$db->sql_freeresult($result3);
				$u_cron[]="user_comments='{$user_comm_data['comm']}'";
				$user_tracker_data[1]=$dt;
			}
			if($u_update[2])
			{
				$sql="SELECT SUM(IF(seeder='1', 1, 0)) seed, SUM(IF(seeder='0', 1, 0)) leech FROM ".TRACKER_PEERS_TABLE." WHERE userid='{$u_update[3]}' AND {$dt} - last_action < {$config['ppkbb_tcdead_time']}".($ppkbb_tcguests_enabled[0] ? " AND guests='0'" : '')."";
				$result3=$db->sql_query($sql, $config['ppkbb_tstat_ctime']*60, "trsl_count#u".$u_update[3]);
				$user_sl_data=$db->sql_fetchrow($result3);
				$user_sl_data['seed']=intval(@$user_sl_data['seed']);
				$user_sl_data['leech']=intval(@$user_sl_data['leech']);
				$u_cron[]="user_seed='{$user_sl_data['seed']}'";
				$u_cron[]="user_leech='{$user_sl_data['leech']}'";
				$user_tracker_data[2]=$dt;
			}
			if(sizeof($u_cron))
			{
				$u_cron[]="user_tracker_data='".implode(' ', $user_tracker_data)."'";
				$sql="UPDATE ".USERS_TABLE." SET ".implode(', ', $u_cron)." WHERE user_id='{$u_update[3]}'";
				$db->sql_query($sql);
			}
			$db->sql_query("DELETE FROM ".TRACKER_CRON_TABLE." WHERE id='{$row['id']}'");
		}
	}
	$db->sql_freeresult($result);
	if($dt-$config['ppkbb_cron_last_cleanup'] > $ppkbb_cron_options[1]*60*60 && $ppkbb_cron_options[0])
	{
		$db->sql_query("DELETE FROM ".TRACKER_CRON_TABLE." WHERE {$dt}-added > ".($ppkbb_cron_options[0]*60));
		set_config('ppkbb_cron_last_cleanup', $dt, 1);
		//purge_tracker_config(true);
	}
}
else
{

}

// Unloading cache and closing db after having done the dirty work.
//unlock_cron();
garbage_collection();

exit();

function hex2bin($str)
{
	$bin = "";
	$i = 0;
	do
	{
		$bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
		$i += 2;
	} while ($i < strlen($str));
	return $bin;
}
?>
